import axios from "axios";
import store from "@/store";
import { ElMessage } from 'element-plus'
let baseUul = "http://localhost:80"
const httpService = axios.create({
    // 路径
    baseURL:baseUul,
    // 超时时间
    timeout:3000
})

// 请求前拦截器
httpService.interceptors.request.use(function (config){
        // 请求前拦截器
        console.log("httpService#token:",store.getters.GET_TOKEN)
        config.headers.token = store.getters.GET_TOKEN
        return config
    }, function(error){
        // 请求错误做出相应
        return Promise.reject(error)
    }
)

// response 拦截器
httpService.interceptors.response.use(
    response => {
      /**
       * code为非20000是抛错 可结合自己业务进行修改
       */
      const res = response.data
      console.log("httpService.interceptors.response.use:", res)
      // debugger
      if (res.code !== 20000) {
        ElMessage.error(res.message)
        return Promise.reject('error')
      } else {
        // 简化了一次数据封装
        return response.data
      }
    },
    error => {
      console.log('err' + error) // for debug
      Message({
        message: error.message,
        type: 'error',
        duration: 5 * 1000
      })
      return Promise.reject(error)
    }
  )

  export const getServerUrl = ()=>{
    return baseUul
  }
  export default httpService